<template>
    <div class="doctor-container">
      <el-card>
        <div class="header-container">
          <span class="doctor-title">
            <i class="el-icon-user"></i> 医生信息
          </span>
          <el-button type="primary" @click="toggleEditMode">{{ editMode ? '保存' : '编辑信息' }}</el-button>
        </div>
        <el-form :model="doctorInfo" label-width="80px">
          <!-- Display fields for doctor information -->
          <el-form-item label="姓名">
            <el-input v-model="doctorInfo.name" :disabled="!editMode"></el-input>
          </el-form-item>
          <el-form-item label="地址">
            <el-input v-model="doctorInfo.address" :disabled="!editMode"></el-input>
          </el-form-item>
          <el-form-item label="联系电话">
            <el-input v-model="doctorInfo.phone" :disabled="!editMode"></el-input>
          </el-form-item>
          <el-form-item label="年龄">
            <el-input v-model="doctorInfo.age" :disabled="!editMode"></el-input>
          </el-form-item>
          <el-form-item label="性别">
            <el-input v-model="doctorInfo.sex" :disabled="!editMode"></el-input>
          </el-form-item>
          <el-form-item label="密码">
            <el-input v-model="doctorInfo.password" :disabled="!editMode"></el-input>
          </el-form-item>
          <el-form-item label="科室">
            <el-input v-model="doctorInfo.department" :disabled="!editMode"></el-input>
          </el-form-item>
        </el-form>
      </el-card>
    </div>
  </template>
  
  <script>
  import axios from 'axios';
  
  export default {
    data() {
      return {
        doctorInfo: {
          name: '',
          address: '',
          phone: '',
          age: '',
          sex: '',
          password: '',
          department: '',
        },
        editMode: false,
      };
    },
    created() {
      // Fetch doctor information when the component is created
      this.fetchDoctorInfo();
    },
    computed: {
      user() {
        return this.$store.state.user;
      },
    },
    methods: {
      async fetchDoctorInfo() {
        try {
          // Fetch doctor information based on user's name
          const response = await axios.get(`http://127.0.0.1:3000/api/doctors/${this.user.name}`);
          this.doctorInfo = response.data;
        } catch (error) {
          console.error('Error:', error);
        }
      },
      toggleEditMode() {
        // Toggle edit mode and save or update doctor information accordingly
        if (this.editMode) {
          this.saveDoctorInfo();
        }
        this.editMode = !this.editMode;
      },
      async saveDoctorInfo() {
        try {
          // Update doctor information if in edit mode
          const response = await axios.put(`http://127.0.0.1:3000/api/doctors/${this.user.id}`, this.doctorInfo);
          console.log('Doctor information updated:', response.data);
        } catch (error) {
          console.error('Error:', error);
        }
      },
    },
  };
  </script>
  
  <style scoped>
  .doctor-container {
    margin: 20px;
  }
  
  .header-container {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 15px;
  }
  
  .doctor-title {
    font-size: 18px;
    font-weight: bold;
    display: flex;
    align-items: center;
  }
  
  .doctor-title i {
    margin-right: 8px;
    font-size: 24px;
  }
  </style>
  